package org.telegram.bot.domain;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import org.telegram.bot.emums.BotCommonEnums;
import org.telegram.bot.utils.BotUtil;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;

import jakarta.validation.constraints.NotEmpty;

/**
 * @Author: wyx
 * @Date: 2024/11/23 22:17
 */
@Data
public class BotKeyboard {
    
    private static final long serialVersionUID = 1L;
    
    private Long chatId;
    @NotEmpty()
    private String config;
    private String content;
    private String buttonRows;
    
    private JSONArray buttonArray;
    
    private InlineKeyboardMarkup inlineKeyboardMarkup;
    
    public BotKeyboard(){}
    
    public BotKeyboard(String config){
        setConfig(config);
    }
    
    public void setConfig(String config) {
        this.config = config;
        JSONObject object = JSONObject.parseObject(config);
        this.chatId = object.getLong("chatId");
        this.content = object.getString(BotCommonEnums.KEYBOARD_CONTENT);
        setButtonRows(object.getString(BotCommonEnums.KEYBOARD_BUTTONROWS));
    }
    
    public void setButtonRows(String buttonRows) {
        this.buttonRows = buttonRows;
        this.buttonArray = JSONArray.parseArray(buttonRows);
        setInlineKeyboardMarkup(BotUtil.generatorKeyboard(this.buttonArray));
    }
    
    public void setInlineKeyboardMarkup(InlineKeyboardMarkup inlineKeyboardMarkup) {
        this.inlineKeyboardMarkup = inlineKeyboardMarkup;
    }
}
